package jp.bsws.action.master;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KengenBean;
import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.TenpoBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.KengenDao;
import jp.bsws.model.KengenModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class KengenMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<KengenBean> kengenBeanList;
	private Integer intTaisyoSeq;

	public void validateUpdateKengen() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
		for(int i =0; i < kengenBeanList.size(); i++){
			KengenBean kengenBean = kengenBeanList.get(i);
			if(intTaisyoSeq == kengenBean.getSeq() && kengenBean.isChkEdit()){
				if (BSWSUtils.isBlankNull(kengenBean.getStrKengenKnj())) {
					addFieldError("kengenBeanList[" + i + "].strKengenKnj", (i + 1) + "行目の権限名称を入力して下さい。");
				}
				break;
			}
		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 権限追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		for(KengenBean kengenBean : kengenBeanList) {
			kengenBean.setSetteiHenkoKbnList(BSWSConstants.setteiHenkoKbnList);
			kengenBean.setTenpoSansyoKbnList(BSWSConstants.tenpoSansyoKbnList);
			kengenBean.setJugyoinSansyoKbnList(BSWSConstants.jugyoinSansyoKbnList);
		}

		KengenBean kengenBean = new KengenBean();
		kengenBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		kengenBean.setSetteiHenkoKbnList(BSWSConstants.setteiHenkoKbnList);
		kengenBean.setTenpoSansyoKbnList(BSWSConstants.tenpoSansyoKbnList);
		kengenBean.setJugyoinSansyoKbnList(BSWSConstants.jugyoinSansyoKbnList);
		kengenBeanList.add(kengenBean);

		return SUCCESS;
	}

	public String updateKengen() throws Exception{

		KengenDao kengenDao = KengenDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//権限情報追加更新対象取得
			for(KengenBean kengenBean : kengenBeanList) {
				int cnt = 0;
				if(kengenBean.getSeq() != null){
					//権限情報更新
					cnt = kengenDao.updateKengen(con, bSWSSession, kengenBean);
				}else{
					if( BSWSUtils.isNotBlankNull(kengenBean.getStrKengenKnj())
							&& BSWSUtils.isNotBlankNull(kengenBean.getStrSetteiHenkoKbn())
							&& BSWSUtils.isNotBlankNull(kengenBean.getStrTenpoSansyoKbn())
							&& BSWSUtils.isNotBlankNull(kengenBean.getStrJugyoinSansyoKbn())
					){
						//権限情報追加
						cnt = kengenDao.insertKengen(con, null, bSWSSession, kengenBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteKengen() throws Exception{

		KengenDao kengenDao = KengenDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			if(intTaisyoSeq != null){
				//権限削除
				int cnt = kengenDao.deleteKengen(con, intTaisyoSeq);
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		//店舗マスタ情報を取得
		getKengenJoho(con);

		//店舗行追加
		KengenBean kengenBean = new KengenBean();
		kengenBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		kengenBean.setStrSetteiHenkoKbn("0");
		kengenBean.setStrTenpoSansyoKbn("0");
		kengenBean.setStrJugyoinSansyoKbn("0");
		kengenBean.setSetteiHenkoKbnList(BSWSConstants.setteiHenkoKbnList);
		kengenBean.setTenpoSansyoKbnList(BSWSConstants.tenpoSansyoKbnList);
		kengenBean.setJugyoinSansyoKbnList(BSWSConstants.jugyoinSansyoKbnList);
		this.kengenBeanList.add(kengenBean);
	}

	private void getKengenJoho(Connection con) throws Exception{

		KengenDao kengenDao = KengenDao.getInstance();
		this.kengenBeanList = new ArrayList<KengenBean>();
		this.kengenBeanList.clear();
		//権限マスタ情報を取得
		ArrayList<KengenModel> kengenList = kengenDao.getKengenJoho(con, null, bSWSSession.getStrGroupCod(), null, null);
		for(int i = 0; i < kengenList.size(); i++){
			KengenBean kengenBean = new KengenBean();
			kengenBean.setSeq(kengenList.get(i).getSeq());
			kengenBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			kengenBean.setStrKengenCod(kengenList.get(i).getStrKengenCod());
			kengenBean.setStrKengenKnj(kengenList.get(i).getStrKengenKnj());
			kengenBean.setStrSetteiHenkoKbn(kengenList.get(i).getStrSetteiHenkoKbn());
			kengenBean.setStrTenpoSansyoKbn(kengenList.get(i).getStrTenpoSansyoKbn());
			kengenBean.setStrJugyoinSansyoKbn(kengenList.get(i).getStrJugyoinSansyoKbn());
			kengenBean.setSetteiHenkoKbnList(BSWSConstants.setteiHenkoKbnList);
			kengenBean.setTenpoSansyoKbnList(BSWSConstants.tenpoSansyoKbnList);
			kengenBean.setJugyoinSansyoKbnList(BSWSConstants.jugyoinSansyoKbnList);
			this.kengenBeanList.add(kengenBean);
		}
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<KengenBean> getKengenBeanList() {
		return kengenBeanList;
	}

	public void setKengenBeanList(List<KengenBean> kengenBeanList) {
		this.kengenBeanList = kengenBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}
}